package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserOnlineTime;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class UserOnlineTimeDaoImpl extends TempDataObjectDao<UserOnlineTime> {
    public static String getUserOnlineTimeSql = "select * from `t_u_user_online_time` where `activityId`=? and `userId`=?";

    public UserOnlineTime getUserOnlineTime(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserOnlineTimeSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserOnlineTimeSql = "update `t_u_user_online_time` set onlineTime=?,lastUpdateTime=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(UserOnlineTime userOnlineTime) {
        userOnlineTime.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userOnlineTime.getOnlineTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userOnlineTime.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userOnlineTime.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userOnlineTime.getUserId()));
        if (getDb().executeNoneQuery(updateUserOnlineTimeSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserOnlineTimeSql = "insert into `t_u_user_online_time`(activityId,userId,onlineTime,lastUpdateTime) values (?,?,?,?)";

    @Override
    protected boolean addData(UserOnlineTime userOnlineTime) {
        userOnlineTime.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userOnlineTime.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userOnlineTime.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userOnlineTime.getOnlineTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userOnlineTime.getLastUpdateTime()));
        if (getDb().executeNoneQuery(insertUserOnlineTimeSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserOnlineTime parseData(ResultSet rs) throws SQLException {
        UserOnlineTime userOnlineTime = new UserOnlineTime();
        userOnlineTime.setActivityId(rs.getInt("activityId"));
        userOnlineTime.setUserId(rs.getLong("userId"));
        userOnlineTime.setOnlineTime(rs.getLong("onlineTime"));
        userOnlineTime.setLastUpdateTime(rs.getLong("lastUpdateTime"));

        return userOnlineTime;
    }

}
